package com.ruoyi.project.system.task.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.task.domain.Delay;
import com.ruoyi.project.system.task.service.DelayService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/system/task/delay")
public class DelayController extends BaseController {

    private String prefix = "system/task/delay";

    @Autowired
    private DelayService delayService;

    /**
     * 跳转到任务延期
     * @return
     */
    @RequiresPermissions("system:delay:view")
    @GetMapping()
    public String task()
    {
        return prefix + "/delay";
    }

    /**
     * 新增延期任务
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }


    /**
     * 修改任务
     */
    @GetMapping("/edit/{delayId}")
    public String edit(@PathVariable("delayId") Long delayId, ModelMap mmap)
    {
        mmap.put("delay", delayService.selectDelayById(delayId));
        return prefix + "/edit";
    }

    /**
     * 修改延期任务
     */
    @RequiresPermissions("system:delay:edit")
    @Log(title = "任务延期", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Delay delay)
    {
        System.out.println(delay.toString());
        return toAjax(delayService.updateDelayTask(delay));
    }

    /**
     * 新增延期任务
     */
    @RequiresPermissions("system:delay:add")
    @Log(title = "任务延期", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Delay delay)
    {
        System.out.println(delay.toString());
        return toAjax(delayService.insertDelay(delay));
    }

    /**
     * 更具条件分页查询
     * @param delay 条件
     * @return
     */
    @RequiresPermissions("system:delay:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Delay delay)
    {
        startPage();
        List<Delay> list = delayService.selectDelayList(delay);
        return getDataTable(list);
    }

    /**
     * 一件导出表
     * @param delay 查询条件
     * @return
     */
    @Log(title = "任务延期", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:delay:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Delay delay)
    {
        List<Delay> list = delayService.selectDelayList(delay);
        ExcelUtil<Delay> util = new ExcelUtil<Delay>(Delay.class);
        return util.exportExcel(list, "任务数据");
    }

    /**
     * 校验延迟任务id是否唯一并且存在
     */
    @PostMapping("/checkDelayTaskIdUnique")
    @ResponseBody
    public String checkDelayTaskIdUnique(Delay delay)
    {
        return delayService.checkDelayTaskIdUnique(delay);
    }

    /**
     * 根据延期任务id删除延期任务
     */
    @RequiresPermissions("system:delay:remove")
    @Log(title = "任务延期", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(delayService.deleteDelayByIds(ids));
    }

    /**
     * 根据延期id查看任务信息
     * @param delayId
     * @param mmap
     * @return
     */
    @GetMapping("/show/{delayId}")
    public String show(@PathVariable("delayId") Long delayId, ModelMap mmap)
    {
        mmap.put("delay", delayService.selectDelayById(delayId));
        return prefix + "/show";
    }

}
